package com.atguigu.user.controller;

import com.atguigu.param.PageParam;
import com.atguigu.param.UserCheckParam;
import com.atguigu.param.UserLoginParam;
import com.atguigu.pojo.User;
import com.atguigu.user.service.UserService;
import com.atguigu.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("check")
    public R check(@RequestBody @Validated UserCheckParam userCheckParam, BindingResult result){

        //检查是否符合检验注解的规则  符合 false  不符合 true
        boolean b=result.hasErrors();
        if(b){
            return R.fail("账号为空，不能使用!");
        }
        return userService.check(userCheckParam);
    }

    @PostMapping("register")
    public R register(@RequestBody @Validated User user,BindingResult result){

        if(result.hasErrors()){
            return R.fail("参数异常，不可注册！");
        }
        return userService.register(user);
    }

    @PostMapping("login")
    public R login(@RequestBody @Validated UserLoginParam userLoginParam,BindingResult result){
        if(result.hasErrors()){
            return R.fail("参数异常,不可登录！");
        }
        return userService.login(userLoginParam);
    }

    @PostMapping("list")
    public R list(@RequestBody PageParam pageParam){

        return userService.list(pageParam);
    }

    @PostMapping("remove")
    public R remove(@RequestBody Integer userId){
        return userService.remove(userId);
    }

    @PostMapping("update")
    public R update(@RequestBody User user){
        return userService.update(user);
    }

    @PostMapping("save")
    public R save(@RequestBody User user){

        return userService.save(user);
    }
}
